package com.voicenews.aggregator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;

public class AdditionTagToArticle {
	static final String[] className = { "tag", "Tag" };
	private URL url = null;
	// "http://giaitri.vnexpress.net/tin-tuc/gioi-sao/trong-nuoc/nhung-guong-mat-tre-du-doan-toa-sang-showbiz-nam-ty-2424524.html";
	// "http://www.pcworld.com.vn/articles/san-pham/thanh-phan-may-tinh/2012/11/1233856/intel-phat-hanh-o-ssd-the-he-thu-3-cho-trung-tam-du-lieu/";
	// "http://vtc.vn/2-366500/xa-hoi/10-ngay-toi-thoi-tiet-se-the-nao.htm";
	// "http://www.anninhthudo.vn/Xa-hoi/Ve-xu-Doai-thon-thuc-da-ong/485519.antd";
	// "http://www.tienphong.vn/xa-hoi/612161/Tuong-Quac-hoi-ngo-thi-si-Viet-Chien-Con-lai-ruou-va-tho-tpp.html";
	// "http://www.thanhnien.com.vn/pages/20130208/nhung-dieu-cam-ky-khi-don-nam-moi-o-trung-quoc.aspx";
	// "http://bongdaplus.vn/La-Liga/Chinh-thuc-Messi-gia-han-voi-Barca-den-nam-2018/81383.bbd";
	// "http://vietnamnet.vn/vn/kinh-te/108979/tien-te-2012--soi-nong-va-thanh-loc.html";
	// "http://www.tinmoi.vn/chu-cay-xang-chui-canh-sat-giai-vay-xe-du-011205265.html";
	// "http://hn.24h.com.vn/bong-da/soc-nang-cau-thu-real-bi-vi-nhu-linh-cau-c48a520363.html";
	// "http://vnexpress.net/gl/the-gioi/nguoi-viet-5-chau/2013/02/nguoi-viet-o-my-hoi-ha-chuan-bi-tet/";
	// "http://news.zing.vn/xa-hoi/nhung-cau-chuyen-nho-ve-chu-tich-nuoc/a302086.html#home_noibat1";
	// "http://vietnamnet.vn/vn/chinh-tri/103510/viet-nam-dung--an-toan-thu-dong-.html";
	// "http://ngoisao.net/tin-tuc/an-choi-xem/2013/02/khong-khi-don-nam-quy-ty-o-cac-quoc-gia-chau-a-230470/";
	// "http://ngoisao.net/tin-tuc/showbiz-viet/2013/02/sao-viet-han-hoan-hat-mung-nam-moi-230306/";
	// "http://kenh14.vn/star/angela-phuong-trinh-sexy-khong-co-nghia-la-phai-coi-20130207113429180.chn";
	// "http://kenh14.vn/doi-song/nhung-hot-girl-tuoi-ran-duoc-ky-vong-but-pha-nam-2013-2013020502331277.chn";
	private TagNode rootNode;

	public AdditionTagToArticle() {

	}

	List<TagNode> getDivsContainClassTag() {
		List<TagNode> divList = new ArrayList<TagNode>();
		TagNode divElements[] = rootNode.getElementsByName("div", true);
		for (int i = 0; divElements != null && i < divElements.length; i++) {

			String classType = divElements[i].getAttributeByName("class");
			if (classType != null
					&& (classType.contains("tag") || classType.contains("Tag")
							|| classType.contains("keyword")
							|| classType.contains("TAG") || classType
								.contains("cx"))) {
				System.out.println("***********" + classType);
				if (!classType.equals("cxtLeft")) {
					// System.out.println(classType);
					divList.add(divElements[i]);
					break;
				}
			}
			// if (divElements[i].getText().toString().contains("Xem thêm:")) {
			// System.out.println("***********************" +
			// divElements[i].getText().toString());
			// divList.add(divElements[i]);
			// }
		}

		return divList;
	}

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
	}

	private static String convertStreamToString(InputStream is)
			throws IOException {
		Charset charset = Charset.forName("utf-8");
		InputStreamReader inputReader = new InputStreamReader(is); //
		inputReader.read();
		BufferedReader reader = new BufferedReader(inputReader);
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	public static void seperator(String s) {
		System.out
				.println("------------------------------------------------------------------------------------------");
		System.out.println(s);
	}

//	public void addTagToArticle(String urlPageString, OntologyManager owlManager) {
//		String base = "http://hust.edu.vn/ontology/news.owl#";
//		String prefix = "\nPREFIX base: <"
//				+ base
//				+ ">"
//				+ "\nPREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> "
//				+ "\nPREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> "
//				+ "\nPREFIX fn: <http://www.w3.org/2005/xpath-functions#> "
//				+ "\nPREFIX owl: <http://www.w3.org/2002/07/owl#> "
//				+ "\nPREFIX xsd: <http://www.w3.org/2001/XMLSchema#>";
//
//		if (urlPageString.contains("tuoitre")) {
//			return;
//		}
//		try {
//			this.url = new URL(urlPageString);
//			HtmlCleaner cleaner = new HtmlCleaner();
//			HttpURLConnection urlConnection = (HttpURLConnection) url
//					.openConnection();
//			urlConnection.addRequestProperty("User-Agent",
//					"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
//
//			// InputStream inputStream = urlConnection.getInputStream();
//			// System.out.println(convertStreamToString(inputStream));
//			this.rootNode = cleaner.clean(urlConnection.getInputStream());
//			TagNode[] aTags = rootNode.getElementsByName("a", true);
//			Set<String> tags = new HashSet<String>();
//			for (int i = 0; i < aTags.length; i++) {
//				String string = aTags[i].getAttributeByName("href");
//				if (string != null && string.contains("tag")) {
//					tags.add(aTags[i].getText().toString().trim());
//				}
//			}
//
//			for (Iterator iterator = tags.iterator(); iterator.hasNext();) {
//				String string = (String) iterator.next();
//				String queryTag = prefix 
//						+ "\n select ?tag where"
//						+ "\n{"
//						+ "\n?tag rdf:type base:Tag ."
//						+ "\n?tag base:hasKeyWord \"" + string + "\" ."
//						+ "\n}";
//				SPARQLResultData result = owlManager.executeSPARQL(queryTag);
//				if (result.getRowSize() > 0) {
//					owlManager
//					.addObjectTriple(result.getRow(0).get(0),
//							"http://hust.edu.vn/ontology/news.owl#isTagOf",
//							"http://hust.edu.vn/ontology/news.owl#"
//									+ urlPageString);
//					System.out.println("ok");
//
//				} else {
//					System.out.println("no");
//					String uriIndividual = "http://hust.edu.vn/ontology/news.owl#"
//							+ UUID.randomUUID().toString();
//					IndividualData id1 = owlManager.createIndividual(
//							uriIndividual,
//							"http://hust.edu.vn/ontology/news.owl#Tag");
//					owlManager.addDataTriple(uriIndividual,
//							"http://hust.edu.vn/ontology/news.owl#hasKeyWord",
//							string);
//					owlManager.addObjectTriple(uriIndividual,
//							"http://hust.edu.vn/ontology/news.owl#isTagOf",
//							"http://hust.edu.vn/ontology/news.owl#"
//									+ urlPageString);
//
//				}
//
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//			System.out.println(url.toString());
//			return;
//		}
//
//	}

}
